package org.example.springboot.controller;

import org.example.springboot.entity.Expert;
import org.example.springboot.service.ExpertService;
import org.example.springboot.dto.ExpertDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reisen7
 * @date 2025/5/29 21:10
 * @description 
 */

@RestController
@RequestMapping("/api/experts")
public class ExpertController {
    @Autowired
    private ExpertService expertService;

    // 获取所有专家（user+expert信息合并）
    @GetMapping
    public List<ExpertDTO> getAllExperts() {
        return expertService.getAllExpertDTOs();
    }

    // 获取单个专家
    @GetMapping("/{userId}")
    public ExpertDTO getExpertByUserId(@PathVariable Long userId) {
        return expertService.getExpertDTOByUserId(userId);
    }

    // 新建/编辑专家资料
    @PostMapping
    public Expert saveExpert(@RequestBody Expert expert) {
        return expertService.save(expert);
    }

    @PutMapping("/{userId}")
    public Expert updateExpert(@PathVariable Long userId, @RequestBody Expert expert) {

        Expert existingExpert = expertService.getExpertDTOByUserId(userId).getExpert();
        System.out.println(expert);
        if (existingExpert != null ) expert.setId(existingExpert.getId());

        return expertService.save(expert);
    }
} 